<template>
	<view class="page">
		<view class="page-header">
			<view class="page-header-content">
				<view class="page-header-title">筛选</view>
			</view>
		</view>
		<view class="page-body form">
			<view class="form-group">
				<view class="form-item">
					<view class="form-item-header">任务号：</view>
					<view class="form-item-body">
						<input 
							v-model="eventTaskNo"
							class="form-control" 
							placeholder="请输入任务号" />
					</view>
				</view>
				<view class="form-item">
					<view class="form-item-header">案卷描述：</view>
					<view class="form-item-body">
						<input 
							class="form-control" 
							v-model="eventDescribe" 
							placeholder="请输入案卷描述" />
					</view>
				</view>
				<view class="form-item">
					<view class="form-item-header">请选择时间：</view>
					<view class="form-item-body">
						<view class="cals">
							<view class="cals-group">
								<view class="cals-item">
									<picker mode="date" class="picker" @change="bindBeginDateChange">
										<view class="cals-item-content">{{ startTime || '开始时间' }}</view>
										<image class="icon" src="@/static/icon/iv_calendar2.png"></image>
									</picker>
								</view>
								<view class="cals-item">
									<picker mode="date" class="picker" @change="bindEndDateChange">
										<view class="cals-item-content">{{ endTime || '结束时间' }}</view>
										<image class="icon" src="@/static/icon/iv_calendar2.png"></image>
									</picker>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="form-item-header">案卷类型：</view>
					<view class="form-item-body">
						<view class="select">
							<picker 
								:range="eventTypes" 
								range-key="name" 
								:value="eventTypeIndex"
								@change="bindTypeChange">
								<view class="selected">
									{{eventType | getSelectedName(eventTypes, 'code') || '请选择'}}
								</view>
								<view class="select-icon">
									<uni-icons type="arrowdown" size="22" color="#c4c4c4" />
								</view>
							</picker>
						</view>
					</view>
				</view>
				<view class="form-item" v-if="eventType">
					<view class="form-item-header">大类名称：</view>
					<view class="form-item-body">
						<view class="select">
							<picker 
								:range="eventLv1Types" 
								range-key="name" 
								:value="eventLv1TypeIndex"
								@change="bindLv1TypeChange">
								<view class="selected">
									{{eventTypeLv1 | getSelectedName(eventLv1Types, 'id') || '请选择'}}
								</view>
								<view class="select-icon">
									<uni-icons type="arrowdown" size="22" color="#c4c4c4" />
								</view>
							</picker>
						</view>
					</view>
				</view>
				
				<view class="form-item" v-if="eventTypeLv1 && eventType">
					<view class="form-item-header">小类名称：</view>
					<view class="form-item-body">
						<view class="select">
							<picker 
								:range="eventLv2Types" 
								range-key="name" 
								:value="eventLv2TypeIndex"
								@change="bindLv2TypeChange">
								<view class="selected">
									{{eventTypeLv2 | getSelectedName(eventLv2Types, 'id') || '请选择'}}
								</view>
								<view class="select-icon">
									<uni-icons type="arrowdown" size="22" color="#c4c4c4" />
								</view>
							</picker>
						</view>
					</view>
				</view>
				
				<view class="form-item">
					<view class="form-item-header">案卷状态：</view>
					<view class="form-item-body">
						<view class="select">
							<picker
								:range="eventStatuses" 
								range-key="name" 
								:value="eventStatusIndex"
								@change="bindStatusChange">
								<view class="selected">
									{{eventStatus | getSelectedName(eventStatuses, 'code') || '请选择'}}
								</view>
								<view class="select-icon">
									<uni-icons type="arrowdown" size="22" color="#c4c4c4" />
								</view>
							</picker>
						</view>
					</view>
				</view>
				
				<view class="form-item" v-if="eventStatus">
					<view class="form-item-header">处理状态：</view>
					<view class="form-item-body">
						<view class="select">
							<picker
								:range="eventDealStatuses" 
								range-key="name" 
								:value="eventDealStatusIndex"
								@change="bindDealStatusChange">
								<view class="selected">
									{{eventDealStatus | getSelectedName(eventDealStatuses, 'code') || '请选择'}}
								</view>
								<view class="select-icon">
									<uni-icons type="arrowdown" size="22" color="#c4c4c4" />
								</view>
							</picker>
						</view>
					</view>
				</view>
				
			</view>
		</view>
		
		<view class="page-footer btns fix-bottom">
			<view class="btns-group">
				<view class="btns-item">
					<button class="btn btn-a" @tap="bindReset">重置</button>
				</view>
				<view class="btns-item" @tap="bindSubmit">
					<button class="btn btn-b">查找</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { queryEventType, queryEventTypelv1, queryEventTypelv2, queryEventStatus, queryEventDealStatus } from '@/api/configure.js';
import { deepClone ,mergeUrls, getUserInfo, formatDateStr } from '@/util/util.js';
import { pathToBase64, base64ToPath } from '@/util/mmmm-image-tools/index.js';
import configUrl from '@/util/url.js';

export default {
	props: {
		data: {
			type: Object,
			default: ()=>{
				return {}
			}
		}
	},
	data(){
		return {
			eventTaskNo: '', //任务号
			eventDescribe: '', //案卷描述,
			startTime: '', //开始时间
			endTime: '', //结束时间 
			eventTypes: [],
			eventTypeIndex: null,
			eventType: '',
			eventLv1Types: [],
			eventLv1TypeIndex: null,
			eventTypeLv1: '',
			eventLv2Types: [],
			eventLv2TypeIndex: null,
			eventTypeLv2: '',
			eventStatuses: [],
			eventStatusIndex: null,
			eventStatus: '',
			eventDealStatuses: [],
			eventDealStatusIndex: null,
			eventDealStatus: ''
		}
	},
	methods: {
		bindBeginDateChange(e) {
			this.startTime = e.detail.value ;
		},
		bindEndDateChange(e) {
			this.endTime = e.detail.value ;
		},
		bindReset() {
			this.eventTaskNo = '';
			this.eventDescribe = '';
			this.startTime = '';
			this.endTime = '';
			
			this.clearType([1, 2]);
			this.clearStatus();
		},
		bindSubmit() {
			let data = {
				eventTaskNo: this.eventTaskNo,
				eventDescribe: this.eventDescribe,
				startTime: this.startTime,
				endTime: this.endTime,
				eventType: this.eventType,
				eventTypeLv1: this.eventTypeLv1,
				eventTypeLv2: this.eventTypeLv2,
				eventStatus: this.eventStatus,
				eventDealStatus: this.eventDealStatus
			}
			
			this.$emit('sendCondition', data);
		},
		async getData() {
			Object.keys(this.data).forEach((key) => {
				this.$data[key] = this.data[key];
			});
			
			await this.getEventType();
			await this.getEventStatus();
			
			if( this.eventType ){
				this.eventTypeIndex = this.getItemIndex(this.eventTypes, this.eventType, 'code');
				
				await this.getEventLv1Types(this.eventType);
			}
			
			if ( this.eventTypeLv1 ) {
				this.eventLv1TypeIndex = this.getItemIndex(this.eventLv1Types, this.eventTypeLv1, 'id');
				
				await this.getEventLv2Types(this.eventTypeLv1);
			}
			
			if ( this.eventTypeLv2 ) {
				this.eventLv2TypeIndex = this.getItemIndex(this.eventLv2Types, this.eventTypeLv2, 'code');
			}
				
			if (this.eventStatus) {
				this.eventStatusIndex = this.getItemIndex(this.eventStatuses, this.eventStatus, 'code');
				
				let eventStatusID = this.eventStatuses[this.eventStatusIndex]['id'];
				
				await this.getEventDealStatuses(eventStatusID);
			}
			
			
			if (this.eventDealStatus) {
				this.eventDealStatusIndex = this.getItemIndex(this.eventDealStatuses, this.eventDealStatus, 'code');
			}
		},
		getItemIndex(arr, value, key = 'code') {
			let index = arr.findIndex((item) => {
				return value == item[key];
			});
			
			
			return index;
		},
		async getEventType() {
			await queryEventType().then( (res) => {
				this.eventTypes = res.data.slice(0);
			} )
		},
		getEventLv1Types(code) {
			return new Promise((resolve, reject) => {
				let data = {
					type: code
				}
				
				queryEventTypelv1(data).then((res) => {
					this.eventLv1Types = res.data.slice(0);
					
					resolve();
				})
			})
		},
		getEventLv2Types(code) {
			let data = {
				parentId: code
			}
			
			queryEventTypelv2(data).then((res) => {
				this.eventLv2Types = res.data.slice(0);
			})
		},
		getEventStatus() {
			return new Promise((resolve, reject) => {
				queryEventStatus().then( (res) => {
					this.eventStatuses = res.data.slice(0);
					
					resolve();
				})
			})
		},
		getEventDealStatuses(code) {
			let data = {
				parentCode: code
			}
			
			queryEventDealStatus(data).then( (res) => {
				this.eventDealStatuses = res.data.slice(0);
			})
		},
		clearType(arr) {
			if(arr.includes(1)){
				this.eventLv1TypeIndex = null;
				this.eventTypeLv1 = '';
				this.eventLv1Types = [];
			}
			
			if(arr.includes(2)) {
				this.eventLv2TypeIndex = null;
				this.eventTypeLv2 = '';
				this.eventLv2Types = [];
			}
		},
		clearStatus() {
			this.eventDealStatuses = [];
			this.eventDealStatusIndex = [];
			this.eventDealStatus = '';
		},
		bindTypeChange(e){
			this.eventTypeIndex = e.detail.value ;
			this.eventType = this.eventTypes[this.eventTypeIndex]['code'];
			
			this.clearType([1, 2]);
			
			this.getEventLv1Types(this.eventType);
		},
		bindLv1TypeChange(e) {
			this.eventLv1TypeIndex = e.detail.value ;
			this.eventTypeLv1 = this.eventLv1Types[this.eventLv1TypeIndex]['id'];
			
			this.clearType([2]);
			this.getEventLv2Types(this.eventTypeLv1);
		},
		bindLv2TypeChange(e) {
			this.eventLv2TypeIndex = e.detail.value ;
			this.eventTypeLv2 = this.eventLv2Types[this.eventLv2TypeIndex]['id'];
		},
		bindStatusChange(e) {
			this.eventStatusIndex = e.detail.value ;
			this.eventStatus = this.eventStatuses[this.eventStatusIndex]['code'];
			this.eventStatusID = this.eventStatuses[this.eventStatusIndex]['id'];
			
			this.clearStatus();
			
			this.getEventDealStatuses(this.eventStatusID);
		},
		bindDealStatusChange(e) {
			this.eventDealStatusIndex = e.detail.value ;
			this.eventDealStatus = this.eventDealStatuses[this.eventDealStatusIndex]['code'];
		},
		init() {
			this.getData();
		}
	},
	filters: {
		getSelectedName(value, arr, key) {
			let ret = '';
			let item = {};
	
			if (value) {
				item = arr.find(item => item[key] == value);
	
				ret = item ? item['name'] : ''
			}
	
			return ret;
		}
	},
	created() {
		this.init();
	},
	mounted() {
		
	}
}
</script>

<style lang="scss">
	.page{
		.page-header{
			padding-top: var(--status-bar-height);
			height: 44px;
			background-color: #f29004;
		}
		.page-header-content{
			position: absolute;
			top: var(--status-bar-height);
			left: 0px;
			right: 0px;
			height: 44px;
			background-color: #f29004;
			.page-header-title{
				padding: 13px;
				font-size: 16px;
				line-height: 120%;
				color: #fff;
				text-align: center;
				font-weight: bold;
			}
		}
	}
	
	.form{
		.form-group{
			.form-item{
				.form-item-header{
					padding: 12rpx 20rpx 12rpx;
					font-size: 24rpx;
					line-height: 120%;
					color: #5d5d5d;
				}
				.form-item-body{
					padding: 0rpx 20rpx;
					.form-control{
						padding: 16rpx 16rpx;
						font-size: 24rpx;
						line-height: 120%;
						color: #5d5d5d;
						border: 1rpx solid #f29004;
						border-radius: 10rpx;
					}
				}
			}
		}
	}
	
	.select{
		border: 1rpx solid #c4c4c4;
		border-radius: 10rpx;
		position: relative;
		top: 0rpx;
		left: 0rpx;
		.selected{
			padding: 16rpx 68rpx 16rpx 16rpx;
			min-height: 36rpx;
			font-size: 24rpx;
			line-height: 120%;
			color: #5d5d5d;
		}
		.select-icon{
			width: 68rpx;
			height: 68rpx;
			position: absolute;
			top: 0rpx;
			right: 0rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
	
	.cals{
		.cals-group{
			display: flex;
			justify-content: space-between;
			position: relative;
			top: 0rpx;
			left: 0rpx;
			&:after{
				margin-left: -10rpx;
				position: absolute;
				left: 50%;
				top: 50%;
				width: 20rpx;
				height: 1rpx;
				content: '';
				background-color: #c4c4c4;
			}
			.picker{
				width: 100%;
				padding: 16rpx 30rpx 16rpx 10rpx;
				box-sizing: border-box;
			}
			.cals-item{
				width: 180rpx;
				border: 1rpx solid #c4c4c4;
				border-radius: 10rpx;
				position: relative;
				top: 0rpx;
				left: 0rpx;
				box-sizing: border-box;
				.cals-item-content{
					height: 28rpx;
					font-size: 24rpx;
					line-height: 120%;
					color: #5d5d5d;
					text-align: center;
				}
				.icon{
					margin-top: -15rpx;
					margin-left: 58rpx;
					position: absolute;
					top: 50%;
					left: 50%;
					width: 26rpx;
					height: 26rpx;
				}
			}
		}
		
	}
	
	.btns{
		background-color: #ffffff;
		.btns-group{
			padding: 17rpx 0rpx;
			display: flex;
			.btns-item{
				width: 50%;
			}
		}
	}
	
	.btn{
		padding: 16rpx 0px;
		font-size: 24rpx;
		line-height: 120%;
		color: #fff;
		text-align: center;
		border: none;
		border-radius: 0rpx;
		background: none;
		&:after{
			display: none;
		}
		&.btn-a{
			background-color: #ffebe1;
			color: #f29004;
		}
		&.btn-b{
			background-color: #f29004;
		}
	}
	
	.fix-bottom{
		position: fixed;
		bottom: 0rpx;
		left: 0rpx;
		right: 0rpx;
	}
</style>
